<?php
namespace app\index\controller;

use app\common\controller\HomeBase;
use app\common\model\AdCategory;
use app\common\model\UrlClickLog as WzapiteModel;
use app\common\model\User as UserModel;
use think\Cache;
use think\Controller;
use think\Db;
use think\Request;
use think\Session;

class SiteCategory extends HomeBase
{

    public function index()
    {
        //最近更新
        $wzapi_new = Db::name('wzapi')->alias('a')->join('user u', 'u.id=a.uid')->join('wzapicate c', 'c.id=a.tid')->where('a.open', 1)->field('u.userhead,u.nickname,a.id,a.uid,a.coverpic,a.title,a.view,a.time,c.template,c.name,a.description,a.content,a.lianjie,c.background')->order('a.time desc')->paginate(20);
        //广告
        $ad=AdCategory::getAdListByLocationId(2);

        $this->assign('ad', $ad);
        $this->assign('wzapi_new', $wzapi_new);

        return view();
    }
     public function lanmu()
    {
        $a=input('cate_alias');
        $alias                = is_valid_character(input('cate_alias')) ? input('cate_alias') : '';
        $cateinfo['showall']  = 0;
        $cateinfo['catename'] = '最新收录';
        $wzapi              = Db::name('wzapi');
        if ($alias) {
            $cateinfo['showall'] = 1;

            $info                 = Db::name('wzapicate')->where('alias', $alias)->find();
            $catealias['tid']     = $info['id'];
            $cateinfo['catename'] = $info['name'];
            $cateinfo['template'] = $info['template'];
            $wzapi_list         = Db::name('wzapi')->alias('a')->join('user u', 'u.id=a.uid')->where('a.open', 1)->where($catealias)->field('a.*,u.nickname')->order('a.id desc')->paginate($this->site_config['c_list_main']);
        } else {
            $wzapi_list = Db::name('wzapi')->alias('a')->join('wzapicate c', 'c.id=a.tid')->join('user u', 'u.id=a.uid')->where('a.open', 1)->field('a.*,c.name,c.template,u.nickname')->order('a.id desc')->paginate($this->site_config['c_list_main']);
        }

        $this->assign('cateinfo', $cateinfo);
        $this->assign('wzapi_list', $wzapi_list);
        return view();
    }
    public function show()
    {
        $id = is_number(input('id')) ? input('id') : '';

        if (empty($id)) {
            return $this->error('亲！你迷路了', 'index/index/index');
        } else {
            $wzapi = Db::name('wzapi');
            $a       = $wzapi->where('open', 1)->find($id);
            if ($a) {
                if ($a['outlink']) {
                    $this->success('正在跳转到外部页面', $a['outlink'], null, 1);
                }

                $wzapi->where("id", $id)->setInc('view', 1);
                $t = $wzapi->alias('a')->join('wzapicate c', 'c.id=a.tid')->join('user m', 'm.id=a.uid')->field('a.*,c.id as cid,c.name,c.template,c.alias,m.id as userid,m.grades,m.point,m.userhead,m.nickname,m.status')->where('a.id', $id)->find();
                $this->assign('t', $t);
                //阅读排行
                $artphb = $wzapi->where('tid', $t['tid'])->order('view desc')->limit($this->site_config['c_list_phb'])->select();
                $this->assign('artphb', $artphb);
                //文章推荐
                $choice['tid']    = $t['tid'];
                $choice['choice'] = 1;
                $artchoice        = $wzapi->where($choice)->order('id desc')->limit($this->site_config['c_view_main'])->select();
                $this->assign('artchoice', $artchoice);

                //查询当前用户是否收藏该文章
                $iscollect  = 0;
                $commentzan = array();
                $uid        = session('userid');
                if ($uid) {
                    $collect = Db::name('collect')->where(array('uid' => $uid, 'sid' => $id, 'type' => 3))->find();
                    if ($collect) {
                        $iscollect = 1;
                    }
                    //查询用户点赞过的文章评论
                    $commentzan = Db::name('zan')->where(array('uid' => $uid, 'type' => 3))->column('sid');

                }
                //评论
                $tptc = Db::name('artcomment')->alias('c')->join('user m', 'm.id=c.uid')->where("fid = {$id}")->order('c.id asc')->field('c.*,m.id as userid,m.grades,m.attestation,m.point,m.userhead,m.nickname')->paginate(10, false, ['query' => Request::instance()->param()]);

                $this->assign('tptc', $tptc);

                $this->assign('iscollect', $iscollect);
                $this->assign('commentzan', $commentzan);

                return view();
            } else {
                return $this->error('亲！你迷路了', 'index/index/index');
            }
        }
    }
	
}
